//
//  UICollectionView+ZBCollectionView.m
//  XZBProduct
//
//  Created by xzb on 2018/7/23.
//  Copyright © 2018年 xzb. All rights reserved.
//

#import "UICollectionView+ZBCollectionView.h"
#import <objc/runtime.h>

static char kDataSourceKey;

@implementation UICollectionView(ZBDataSource)

- (ZBDataSource *)zb_dataSource
{
    ZBDataSource *ds = objc_getAssociatedObject(self, &kDataSourceKey);
    if (!ds) {
        ds = [[ZBDataSource alloc] initWithCollectionView:self];
        objc_setAssociatedObject(self, &kDataSourceKey, ds, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        if (!self.dataSource) {
            self.dataSource = ds;
            self.delegate = ds;
        }
    }
    return ds;
}

- (ZBCollectionData *)zb_collectionData {
    return self.zb_dataSource.collectionData;
}
@end
@implementation UICollectionView (ZBCollectionView)

+ (instancetype)zb_initialize
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.headerReferenceSize = CGSizeMake([UIScreen height], 40);
    return [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
}

- (void)zb_configTableView:(void(^)(ZBCollectionProtoFactory *config))block{
    
    ZBCollectionProtoFactory *config = [[ZBCollectionProtoFactory alloc] init];
    if (block) block(config);
    NSArray *configs = [config install];
    [configs enumerateObjectsUsingBlock:^(NSDictionary *config, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger type = (config[@"type"] ? [config[@"type"] integerValue] : 1);
        if (type == ZBCollectionViewConfigTypeCell) {
            if (config[@"nibName"]) {
                UINib *nib = [UINib nibWithNibName:config[@"nibName"] bundle:nil];
                [self.zb_dataSource registerNib:nib
                                      cellClass:NSClassFromString(config[@"class"])
                                     identifier:config[@"proto"]];
            } else if(config[@"nib"]) {
                [self.zb_dataSource registerNib:config[@"nib"]
                                      cellClass:NSClassFromString(config[@"class"])
                                     identifier:config[@"proto"]];
            } else {
                [self.zb_dataSource registerCell:NSClassFromString(config[@"class"])
                                      identifier:config[@"proto"]];
            }
        } else if (type == ZBCollectionViewConfigTypeHeaderView) {
            if (config[@"nibName"]) {
                UINib *nib = [UINib nibWithNibName:config[@"nibName"] bundle:nil];
                [self.zb_dataSource registerHeaderNib:nib viewClass:NSClassFromString(config[@"class"]) identifier:config[@"proto"]];
            } else {
                [self.zb_dataSource registerHeaderView:NSClassFromString(config[@"class"]) identifier:config[@"proto"]];
            }
        } else if (type == ZBCollectionViewConfigTypeFooterView) {
            if (config[@"nibName"]) {
                UINib *nib = [UINib nibWithNibName:config[@"nibName"] bundle:nil];
                [self.zb_dataSource registerFooterNib:nib viewClass:NSClassFromString(config[@"class"]) identifier:config[@"proto"]];
            } else {
                [self.zb_dataSource registerFooterView:NSClassFromString(config[@"class"]) identifier:config[@"proto"]];
            }
        }
    }];
    
}
- (void)zb_setup:(void(^)(ZBCollectionMaker *maker))block{
    
    ZBCollectionMaker *maker = [[ZBCollectionMaker alloc] init];
    block(maker);
    
    NSArray *sections = [maker install];
    
    [self.zb_dataSource updateAll:sections];    
    
    [self reloadData];
}

- (void)zb_replaceSection:(NSString *)tag block:(void(^)(ZBCollectionMaker *maker))block{
    ZBSection *section = [self.zb_collectionData sectionForKey:tag];
    if (section) {
        ZBCollectionMaker *maker = [[ZBCollectionMaker alloc] init];
        if (block) {
            block(maker);
        }
        NSArray *sections = [maker install];
        if ([sections count] > 0) {
            ZBSection *newSection = sections[0];
            [self.zb_collectionData replaceSection:section newSection:newSection];
        }
    }
}

- (void)zb_appendSection:(NSString *)tag block:(void(^)(ZBCollectionMaker *maker))block
{
    ZBSection *section = [self.zb_collectionData sectionForKey:tag];
    if (section) {
        ZBCollectionMaker *maker = [[ZBCollectionMaker alloc] init];
        maker.section(tag);
        if (block) {
            block(maker);
        }
        NSArray *sections = [maker install];
        if ([sections count] > 0) {
            ZBSection *sectionDataToAppend = sections[0];
            [section appendRows:sectionDataToAppend.rows];
        }
    }
}

- (void)zb_appendRowsForSection:(NSString *)tag block:(void(^)(ZBCollectionMaker *maker))block
{
    ZBSection *section = [self.zb_collectionData sectionForKey:tag];
    if (section) {
        ZBCollectionMaker *maker = [[ZBCollectionMaker alloc] init];
        maker.section(tag);
        if (block) {
            block(maker);
        }
        NSArray *sections = [maker install];
        if ([sections count] > 0) {
            ZBSection *sectionDataToAppend = sections[0];
            [section appendRows:sectionDataToAppend.rows];
        }
    }
}


@end
